---
layout: "default"
title: "Unmanaged"
description: "Swift documentation for 'Unmanaged': A type for propagating an unmanaged object reference."
keywords: "Unmanaged,struct,swift,documentation,autorelease,fromOpaque,passRetained,passUnretained,release,retain,takeRetainedValue,takeUnretainedValue,toOpaque"
root: "/v1.2"
---

<div class="intro-declaration"><code class="language-swift">struct Unmanaged&lt;T&gt;</code></div>

<div class="discussion comment">
    <p>A type for propagating an unmanaged object reference.</p>

<p>When you use this type, you become partially responsible for
keeping the object alive.</p>
</div>

<table class="standard">



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>






<h3>Static Methods</h3>
<div class="declaration" id="func-fromopaque_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-fromopaque_">static func fromOpaque(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-fromopaque_"><div class="p">
    <p>Unsafely turn an opaque C pointer into an unmanaged
class reference.</p>

<p>This operation does not change reference counts.</p>

<pre><code class="language-swift">let str: CFString = Unmanaged.fromOpaque(ptr).takeUnretainedValue()</code></pre>

    <h4>Declaration</h4>    
    <code class="language-swift">static func fromOpaque(value: COpaquePointer) -&gt; Unmanaged&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-passretained_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-passretained_">static func passRetained(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-passretained_"><div class="p">
    <p>Create an unmanaged reference with an unbalanced retain.
The object will leak if nothing eventually balances the retain.</p>

<p>This is useful when passing an object to an API which Swift
does not know the ownership rules for, but you know that the
API expects you to pass the object at +1.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static func passRetained(value: T) -&gt; Unmanaged&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-passunretained_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-passunretained_">static func passUnretained(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-passunretained_"><div class="p">
    <p>Create an unmanaged reference without performing an unbalanced
retain.</p>

<p>This is useful when passing a reference to an API which Swift
does not know the ownership rules for, but you know that the
API expects you to pass the object at +0.</p>

<pre><code class="language-swift">CFArraySetValueAtIndex(.passUnretained(array), i,
                       .passUnretained(object))</code></pre>

    <h4>Declaration</h4>    
    <code class="language-swift">static func passUnretained(value: T) -&gt; Unmanaged&lt;T&gt;</code>
    
    
</div></div>
</div>

<h3>Instance Methods</h3>
<div class="declaration" id="func-autorelease">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-autorelease">func autorelease()</a>
        
<div class="comment collapse" id="comment-func-autorelease"><div class="p">
    <p>Perform an unbalanced autorelease of the object.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func autorelease() -&gt; Unmanaged&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-release">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-release">func release()</a>
        
<div class="comment collapse" id="comment-func-release"><div class="p">
    <p>Perform an unbalanced release of the object.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func release()</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-retain">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-retain">func retain()</a>
        
<div class="comment collapse" id="comment-func-retain"><div class="p">
    <p>Perform an unbalanced retain of the object.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func retain() -&gt; Unmanaged&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-takeretainedvalue">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-takeretainedvalue">func takeRetainedValue()</a>
        
<div class="comment collapse" id="comment-func-takeretainedvalue"><div class="p">
    <p>Get the value of this unmanaged reference as a managed
reference and consume an unbalanced retain of it.</p>

<p>This is useful when a function returns an unmanaged reference
and you know that you&#39;re responsible for releasing the result.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func takeRetainedValue() -&gt; T</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-takeunretainedvalue">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-takeunretainedvalue">func takeUnretainedValue()</a>
        
<div class="comment collapse" id="comment-func-takeunretainedvalue"><div class="p">
    <p>Get the value of this unmanaged reference as a managed
reference without consuming an unbalanced retain of it.</p>

<p>This is useful when a function returns an unmanaged reference
and you know that you&#39;re not responsible for releasing the result.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func takeUnretainedValue() -&gt; T</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-toopaque">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-toopaque">func toOpaque()</a>
        
<div class="comment collapse" id="comment-func-toopaque"><div class="p">
    <p>Unsafely turn an unmanaged class reference into an opaque
C pointer.</p>

<p>This operation does not change reference counts.</p>

<pre><code class="language-swift">let str: CFString = Unmanaged.fromOpaque(ptr).takeUnretainedValue()</code></pre>

    <h4>Declaration</h4>    
    <code class="language-swift">func toOpaque() -&gt; COpaquePointer</code>
    
    
</div></div>
</div>


